﻿
using System;
using System.Collections.Generic;
using System.Linq;
using Domain.Entity;

namespace EmailMarketingDao
{
   public class SendEmailAcountDao
    {
       public static List<SendEmailAcount> GetAllAcount()
       {
           using (var db = new DBDataContext())
           {
               return db.SendEmailAcount.ToList();
           }
       } 
       /// <summary>
       /// 获取某邮局可用的发送账号
       /// </summary>
       /// <param name="smtp"></param>
       /// <returns></returns>
       public static SendEmailAcount GetAvailableSendAcountBySMTP(string smtp)
       {
           using (var db = new DBDataContext())
           {
               var acount = db.SendEmailAcount.FirstOrDefault(x => x.DayMaxSendCount>x.HasSendCount&&x.SMTP==smtp&&x.Status==1);
               return acount;
           }
       }
       /// <summary>
       /// 更新发送账号已经发送的数量
       /// </summary>
       /// <param name="sendAcount"></param>
       /// <param name="count"></param>
       public static void UpdateSendAcountHasendCount(string sendAcount,int count)
       {
           using (var db = new DBDataContext())
           {
               var acount = db.SendEmailAcount.FirstOrDefault(x => x.Acount == sendAcount);
               if (acount != null) acount.HasSendCount = count;
               if (count > 0) acount.LastLogin = DateTime.Now;//更新最后使用时间
               db.SubmitChanges();
           }
       }
       /// <summary>
       /// 更新上次登录的时间
       /// </summary>
       /// <param name="sendAcount"></param>
       /// <param name="time"></param>
       public static void UpdateLastLoginTime(string sendAcount, DateTime time)
       {
           using (var db = new DBDataContext())
           {
               var acount = db.SendEmailAcount.FirstOrDefault(x => x.Acount == sendAcount);
               if (acount != null) acount.LastLogin = time;
               db.SubmitChanges();
           }
       }
    }
}
